﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RaProWorld
{
    class KomaZiegen_goat
    {
        protected Vector2 position;
        public Vector2 Position
        {
            get { return position; }
        }
        protected float size;
        public float Size
        {
            get { return size; }
        }
        public Vector2 center;
        public Vector2 Center
        {
            get { return center; }
        }
        protected Texture2D sprite;
        protected List<Vector2> waypoints;
        protected Vector2 target;
        protected float speed;
        protected List<KomaZiegen_goat> goats;
        protected List<Rectangle> toasts;
        protected int toastCount;
        public bool isShocked;
        public Vector2 shockedTarget;

        public KomaZiegen_goat(Vector2 position, Texture2D sprite, List<KomaZiegen_goat> goats, List<Rectangle> toasts, List<Vector2> waypoints)
        {
            this.position = position;
            this.sprite = sprite;
            this.goats = goats;
            this.toasts = toasts;
            this.waypoints = waypoints;

            size = MathHelper.Lerp(150.0f, 250.0f, (float)(new Random().NextDouble()));
            target = this.waypoints[0];
            center = new Vector2(this.position.X + size / 2, this.position.Y + size / 2);
            speed = 1 / size * 200;
            toastCount = 0;
            isShocked = false;
        }

        public void Update(KomaZiegen_objectMap objMap, Score ziegenScore)
        {
            if (Vector2.Distance(position, target) <= speed)
            {
                try
                {
                    target = waypoints[waypoints.IndexOf(target) + 1];
                }
                catch (ArgumentOutOfRangeException)
                {
                    target = waypoints[0];
                }
            }

            Vector2 targetVec;
            if (this.isShocked)
            {
                targetVec = shockedTarget;
                targetVec.Normalize();
                speed -= 0.02f;
                if (speed <= 0.0f) isShocked = false;
            }
            else
            {
                if (speed < 1 / size * 300) speed += 0.15f;
                targetVec = target - position;
                targetVec.Normalize();
                /*
                int hitGoatIndex = objMap.goatIndexAt(position.X, position.Y);
                if (hitGoatIndex != -1 && !goats[hitGoatIndex].Equals(this))
                {
                    targetVec -= (goats[hitGoatIndex].position - this.position);
                    targetVec.Normalize();
                }
                */
            }
            position += targetVec * speed;
            center = new Vector2(this.position.X + size / 2, this.position.Y + size / 2);

            if (this.isShocked && objMap.toastIndexAt(center.X, center.Y) != -1 && this.toastCount < 50)
            {
                try
                {
                    toastCount++;
                    ziegenScore.addScore(1);
                    toasts.RemoveAt(objMap.toastIndexAt(center.X, center.Y));
                }
                catch (ArgumentOutOfRangeException)
                {
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(sprite,
                position,
                new Rectangle(0, 0, sprite.Width / 2, sprite.Height / 2),
                Color.White,
                0.0f,
                new Vector2((float)sprite.Width / 2, (float)sprite.Height / 2),
                size / 100.0f,
                ((target.X - position.X) >= 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None),
                0);
            if (this.isShocked)
            {
                SpriteFont blaFont = Program.raProWorld.Content.Load<SpriteFont>("Fonts\\TextboxFont");
                spriteBatch.DrawString(blaFont,"SHOCK!",position,Color.Black);
            }
        }
    }
}